package model;

import java.util.ArrayList;

import init.DBManagement;


import model.beans.UsuarioBean;

public class User {
	private String str_username;
	private String str_password;

	
	/**
	 * 
	 * @param str_username
	 * @param str_password
	 */
	public User( String str_username, String str_password ){
		this.str_username = str_username;
		this.str_password = str_password;
	}
	
	
	/**
	 * 
	 * @return
	 */
	public UsuarioBean validating( ) {
		UsuarioBean ub_retVal = null;
		
	    String str_validating =
	    	" SELECT   USR_TECH_KEY,                                  " +
	    	"          LOGIN,                                         " +
	    	"          NOMBRE,                                        " +
	    	"          PASSWORD,                                       " +
	    	"          PER_MINUTA,                                       " +
	    	"          PER_PLANES,                                       " +
	    	"          PER_MST,                                       " +
	    	"          PER_ESTAD                                       " +
	    	" FROM BASUSRB " +
	        " WHERE LOGIN = '"+str_username+"' AND                    " +
	        "       PASSWORD = '"+str_password+"'                     ";
	    DBManagement database = new DBManagement();
	    database.executeQuery( str_validating );
	    if( database.getNumRows() == 1 ){
	    	ub_retVal = new UsuarioBean();
	    	ub_retVal.setId(database.getItem(0, 0));
	    	ub_retVal.setLogin(str_username);
	    	ub_retVal.setNombre(database.getItem(0, 2));
	    	ub_retVal.setTipo(UsuarioBean.MAESTRO);
	    	ub_retVal.setPer_minutas( Integer.parseInt( database.getItem(0, 4) ) );
	    	ub_retVal.setPer_planes( Integer.parseInt( database.getItem(0, 5) ) );
	    	ub_retVal.setPer_mst(Integer.parseInt( database.getItem(0, 6) ));
	    	ub_retVal.setPer_estad( Integer.parseInt( database.getItem(0, 7) ) );
	    	//ub_retVal.setMap_entidad(getEntidad(ub_retVal.getId()));
	    }else{
	    	str_validating = 
	    		" SELECT NOMBRE "+
				" FROM   BASALMV "+
				" WHERE LOGIN = '"+str_username+"' AND "+
				"              PASSWORD = '"+str_password+"'   ";
	    	database.executeQuery( str_validating );
	    	if(database.getNumRows() == 1 ){
		    	ub_retVal = new UsuarioBean();
		    	ub_retVal.setId(str_username);
		    	ub_retVal.setLogin(str_username);
		    	ub_retVal.setNombre(database.getItem(0, 0));
		    	ub_retVal.setTipo(UsuarioBean.ALUMNO);
	    	}
	    }
	    database = null;
	    return ub_retVal;
	}
	
	
	/**
	 * 
	 * @param str_name
	 * @param str_academy
	 * @return
	 */
	public static ArrayList search(String str_name, String str_academy){
		ArrayList arrL_retVal = new ArrayList();
		UsuarioBean ub_retVal = null;
		
	    String str_validating =
	        " SELECT  USR_TECH_KEY, "+
	        "       NOMBRE "+
	        " FROM   BASUSRB "+
	        "              INNER JOIN BASUSAR "+
	        "			   ON USR_TECH_KEY = MAESTRO_TK AND "+
	        "			   	   ACADEMIA_TK = '"+str_academy+"' "+
	        " WHERE upper(NOMBRE) LIKE upper('%"+str_name+"%') or "+ 
	        "	     upper(LOGIN) LIKE upper('%"+str_name+"%') ";
	    DBManagement database = new DBManagement();
	    database.executeQuery( str_validating );
	    for( int int_i = 0; int_i < database.getNumRows(); int_i++){
	    	ub_retVal = new UsuarioBean();
	    	ub_retVal.setId(database.getItem(int_i, 0));
	    	ub_retVal.setNombre(database.getItem(int_i, 1));
	    	arrL_retVal.add(ub_retVal);
	    }
	    database = null;
		return arrL_retVal;
	}
	
	/**
	 * Actualizacion de Contrasenia
	 * 
	 * 
	 * @param map_password
	 * @return
	 */
	public static boolean passwordUpdate(
			String str_login,
			String str_password,
			String str_newPassword){
		boolean bool_update = false;
		
		String str_validate = 
			" SELECT   login,                           " +
			"          nombre,                          " +
			"          password                         " +
			" FROM BASUSRB                              " +
			"      WHERE LOGIN = '"+ str_login +"'      " ;
		
		
		
		DBManagement db = new DBManagement();
		db.executeQuery(str_validate);
		String str_pass = db.getItem(0,2);
		
		if(str_password.equals(str_pass)){
			String str_update = 
				" UPDATE BASUSRB                          " +
				" SET PASSWORD =   '"+str_newPassword+"'  " +
				" WHERE login=  '"+str_login+"'           ";
			
			db.executeQuery(str_update);
			bool_update = true;
		}
		return bool_update;
	}
}
